home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-05-24 | 4.4 KB | 177 lines | [TEXT/CWIE] |
- // stolen from PowerPlant, adapted to C
-
- #include "UtilsSys7.h"
- #include "SimResIDs.h"
-
- #include "GrowZone.h"
- #include "AEHandlers.h"
- #include "SimUtils.h"
-
- #define kCushionSize 30720UL
- #define kReserveSize 40960UL
-
- static GrowZoneUPP gMyGrowZone_UPP = nil;
- static Handle gLocalReserve = nil;
- static Boolean gGiveWarning = false;
-
-
- static Boolean MemoryIsLow(void);
- static UInt32 DoGrowZone(UInt32 inBytesNeeded);
- static UInt32 UseLocalReserve(void);
- static pascal long theRealGZ(Size inBytesNeeded);
-
- #pragma segment Init
- // ---------------------------------------------------------------------------
- // • LGrowZone
- // ---------------------------------------------------------------------------
- // Constructor
- //
- // Allocates a memory reserve of the specified size and installs
- // a GrowZone function
- //
- // Installs the LGrowZone object in the Periodical Repeater queue so
- // that its SpendTime() function will get called each time through the
- // main event loop.
-
- void InitGrowZone(void)
- {
- gLocalReserve = NewHandle(kReserveSize);
- gMyGrowZone_UPP = NewGrowZoneProc(theRealGZ);
- SetGrowZone(gMyGrowZone_UPP);
- }
-
- #pragma segment CleanUp
-
- // ---------------------------------------------------------------------------
- // • ~LGrowZone
- // ---------------------------------------------------------------------------
- // Destructor
- //
- // Deallocates memory reserve and de-installs GrowZone function
-
- void CleanupGrowZone(void)
- {
- if (gLocalReserve) {
- DisposeHandle(gLocalReserve);
- gLocalReserve = nil;
- }
-
- SetGrowZone(nil);
- DisposeRoutineDescriptor(gMyGrowZone_UPP);
- gMyGrowZone_UPP = nil;
- }
-
- #pragma segment Main
- // ---------------------------------------------------------------------------
- // • SpendTime
- // ---------------------------------------------------------------------------
- // Try to reallocate our memory reserve if necessary and warn user if
- // memory is getting low
- //
- // Called each time through the main event loop
-
- void GrowZone_Idle(void)
- {
- if (MemoryIsLow()) {
- if (MaxBlock() > kReserveSize + 2048) {
- if (gLocalReserve == nil)
- gLocalReserve = NewHandle(kReserveSize);
- else
- ReallocateHandle(gLocalReserve, kReserveSize);
-
- if (!MemError())
- gGiveWarning = false;
- }
- }
-
- if (gGiveWarning) {
- gGiveWarning = false;
- if (Get1Resource('ALRT', kALRT_GZOUTOFMEM))
- (void) CautionAlert_AE(kALRT_GZOUTOFMEM, myStdFilterProcNoCancel, myIdleFunct);
- }
- }
-
- // ---------------------------------------------------------------------------
- // • MemoryIsLow
- // ---------------------------------------------------------------------------
- // Return whether our memory reserve has been used
- //
- // Clients can call this routine if they wish to behave differently in
- // low memory situations. For example, a program could disable the "New"
- // and "Open" commands to prevent new Documents from being created when
- // memory is low.
-
- Boolean MemoryIsLow(void)
- {
- return (gLocalReserve == nil || *gLocalReserve == nil);
- }
-
- Boolean IsMemoryAvailable(UInt32 needed)
- {
- if (MemoryIsLow())
- return false;
-
- if (needed + kCushionSize < MaxBlock())
- return true;
- else {
- return needed + kCushionSize < MaxBlock();
- }
- }
-
-
- // ---------------------------------------------------------------------------
- // • DoGrowZone
- // ---------------------------------------------------------------------------
- // Called by our Toolbox GrowZone function when the system needs more
- // memory
-
- UInt32 DoGrowZone(UInt32 /*inBytesNeeded*/)
- {
-
- return UseLocalReserve();
- }
-
- // ---------------------------------------------------------------------------
- // • UseLocalReserve
- // ---------------------------------------------------------------------------
- // Empty our local memory reserve in order to free up some memory
-
- UInt32 UseLocalReserve(void)
- {
- UInt32 bytesFreed = 0;
-
- if ( gLocalReserve &&
- *gLocalReserve &&
- gLocalReserve != GZSaveHnd() ) {
-
- EmptyHandle(gLocalReserve);
- bytesFreed = kReserveSize;
- gGiveWarning = true;
- }
-
- return bytesFreed;
- }
-
-
- // ---------------------------------------------------------------------------
- // • GrowZoneCallBack [static]
- // ---------------------------------------------------------------------------
- // This is the "real" GrowZone function registered with the System. It
- // sets up the A5 world (68K) so we can access globals, then calls a
- // virtual function for the LGrowZone class.
-
- pascal long theRealGZ(Size inBytesNeeded)
- {
- #if !GENERATINGCFM
- long theA5 = SetCurrentA5();
- #endif
- UInt32 bytesFreed = DoGrowZone(inBytesNeeded);
-
- #if !GENERATINGCFM
- (void) SetA5(theA5);
- #endif
-
- return bytesFreed;
- }
-
-